package ch.bfh.mobicomp;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.SensorManager;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

/**
 * 
 * @author Dominik
 * 
 */

public class SensorService extends Service implements SensorListener {
	private CompassEventListener compassEventListener;
	private AccelerometerEventListener accelerometerEventListener;
	private LightEventListener lightEventListener;
	
	private final IBinder mBinder = new LocalBinder();

	public static final String SERVICE_ON = "ch.bfh.mobicomp.SensorService.SERVICE_ON";
	public static final String SERVICE_OFF = "ch.bfh.mobicomp.SensorService.SERVICE_OFF";
	public static final String SERVICE_CANCELED = "ch.bfh.mobicomp.SensorService.SERVICE_CANCELED";
	public static final String CONFIG_CHANGED = "ch.bfh.mobicomp.SensorService.CONFIG_CHANGED";

	private static final String SP_NAME = "ch.bfh.mobicomp.Alarm";
	private static final String SP_KEY_ALARM_LIGHT = "alarmLight";
	private static final String SP_KEY_ALARM_ACCELERATION = "alarmAcceleration";
	private static final String SP_KEY_ALARM_COMPASS = "alarmCompass";

	class LocalBinder extends Binder {
		SensorService getService() {
			return SensorService.this;
		}
	}
	
	@Override
	public void onCreate() {
		SensorManager manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		compassEventListener = new CompassEventListener(manager);
		accelerometerEventListener = new AccelerometerEventListener(manager);
		lightEventListener = new LightEventListener(manager);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.d("SENSOR", "Start Comment send!");

		if (intent.getAction() != null) {
			// SERVICE ON
			if (intent.getAction().equals(SERVICE_ON)) {
				Log.d("SENSOR", SERVICE_ON);

				SharedPreferences pref = getSharedPreferences(SP_NAME,
						MODE_PRIVATE);

				if (pref.getBoolean(SP_KEY_ALARM_COMPASS, false))
					compassEventListener.addCompassActionListener(this);
				if (pref.getBoolean(SP_KEY_ALARM_ACCELERATION, false))
					accelerometerEventListener
							.addAccelerometerActionListener(this);
				if (pref.getBoolean(SP_KEY_ALARM_LIGHT, false))
					lightEventListener.addLightActionListener(this);

				Toast.makeText(this, "Sensor service started!",
						Toast.LENGTH_SHORT).show();
			}
			// SERVICE OFF
			if (intent.getAction().equals(SERVICE_OFF)) {
				Log.d("SENSOR", SERVICE_OFF);

				compassEventListener.removeCompassActionListener(this);
				accelerometerEventListener
						.removeAccelerometerActionListener(this);
				lightEventListener.removeLightActionListener(this);

				Toast.makeText(this, "Sensor service stopped!",
						Toast.LENGTH_SHORT).show();
			}
			// SERVICE CANCELED
			if (intent.getAction().equals(SERVICE_CANCELED)) {
				Log.d("SENSOR", SERVICE_CANCELED);
			}
			// SERVICE CANCELED
			if (intent.getAction().equals(CONFIG_CHANGED)) {
				Log.d("SENSOR", CONFIG_CHANGED);
			}

		}

		return START_STICKY;
	}

	@Override
	public void onDestroy() {
		// Tell the user we stopped.
		Toast.makeText(this, "Alarm service stopped!", Toast.LENGTH_SHORT)
				.show();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}

	@Override
	public void onSensorChanged() {
		Log.d("SENSOR", "onSensorChanged");
		sendBroadcast(new Intent(AlarmManager.INTENT_ALARM_ON));
	}
}
